package com.hxf.web.action.user;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.PublicUtil;
import com.hxf.dto.AdminUserDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.RestaurentUserDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

/**
 * 会员登录Action
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */ 
@Results({@Result(name = "success", location = "../common/requestSend.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "userFilterMsg", location = "../common/userFilterMsg.jsp")})
public class LoginAction extends WebActionBase {
	
	private static final long serialVersionUID = 1L;
	private String userName;
	private String userPwd;
	private String userId;
	private String newUserPwd;
	
	/**
	 * 餐厅管理员登录
	 * @return
	 */
	public String restaurentUserLogin() {
		RestaurentUserDto rud = new RestaurentUserDto();
		rud.setAccount(userName);
		rud.setPwd(userPwd);

		try {
			int result = super.serviceFacade.getRestaurantUserService().restaurantUserLogin(rud);
			if (result == 0) {
				rud = super.serviceFacade.getRestaurantUserService().findRestaurentUser(rud);
				getSession().setAttribute("restaurentUserDtoLogin", rud);
				getSession().setAttribute("adminUserDto", null);
				
				getSession().setAttribute("restaurantId", rud.getRestaurantId());//获取 餐厅 id
			}

			println(result); 
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 联盟管理登录
	 * @return
	 */
	public String adminUserLogin() {
		AdminUserDto ud = new AdminUserDto();
		ud.setUserName(userName);//用户名
		ud.setUserPwd(userPwd);//密码
		try {
			int result = super.serviceFacade.getAdminUserService().loginUser(ud);
			if (result == 0) {
				//UserDto uDto = (UserDto)super.serviceFacade.getUserService().get(UserDto.class, ud.getUserId());
				AdminUserDto adminDto = (AdminUserDto)super.serviceFacade.getAdminUserService().get(AdminUserDto.class, ud.getId());
				//uDto.setLoginVersion(String.valueOf(System.currentTimeMillis()));
				//uDto.setLoginTime(DateUtil.getDatePatternM()); 
				//uDto.setLoginStatus(UserDto.LOGIN_STATUS_1);
				//uDto.setLoginIp(getRemoteIp());
				//super.serviceFacade.getUserService().update(uDto);
				List<RestaurantDto> listRestDto = (List<RestaurantDto>) super.serviceFacade.getRestaurantService().findByRestaurantId(adminDto.getId());
				RestaurantDto restDto = null;
				if (listRestDto != null && 0!=listRestDto.size()) {
					//餐厅Id
					restDto = listRestDto.get(0);
					getSession().setAttribute("restaurantId", restDto.getId());//获取餐厅 编号
				}
				getSession().setAttribute("adminUserDto", adminDto);
				getSession().setAttribute("restaurentUserDtoLogin", null);
			}
			println(result); 
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	/**
	 * 前台登陆
	 */
	public String loginUser() {
		UserDto ud = new UserDto();
		ud.setUserName(userName);//用户名
		ud.setUserPwd(userPwd);//密码
		try {
			int result = super.serviceFacade.getUserService().loginUser(ud);
			if (result == 0) {
				UserDto uDto = (UserDto)super.serviceFacade.getUserService().get(UserDto.class, ud.getUserId());
				uDto.setLoginVersion(String.valueOf(System.currentTimeMillis()));
				uDto.setLoginTime(DateUtil.getDatePatternM()); 
				uDto.setLoginStatus(UserDto.LOGIN_STATUS_1);
				uDto.setLoginIp(getRemoteIp());
				super.serviceFacade.getUserService().update(uDto);
				List<RestaurantDto> listRestDto = (List<RestaurantDto>) super.serviceFacade.getRestaurantService().findByRestaurantId(uDto.getUserId());
				RestaurantDto restDto = null;
				if (listRestDto != null && 0!=listRestDto.size()) {
					//餐厅Id
					restDto = listRestDto.get(0);
					getSession().setAttribute("restaurantId", restDto.getId());//获取餐厅 编号
				}
				getSession().setAttribute("userDto", uDto);
			}
			println(result); 
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}	
	
	/**
	 * 退出
	 * @return
	 */
	public String logout() {
		//getSession().setAttribute("userDto", null);
		//getSession().removeAttribute("userDto");
		Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
		Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
		
		if(null != adminUserDto){
			getSession().setAttribute("adminUserDto", null);
			getSession().removeAttribute("adminUserDto");
			requestURL = getWebUrl()+"/admin/login.jsp";
		}else if(null != restaurentUserDto){
			getSession().setAttribute("restaurentUserDto", null);
			getSession().removeAttribute("restaurentUserDto");
			requestURL = getWebUrl()+"/login.jsp";
		}
		//requestURL = getWebUrl()+"/login.jsp";
		return SUCCESS;
	}
	
	/**
	 * 修改密码界面
	 * @return
	 */
	public String pwdUpdate() {
		ServletActionContext.getRequest().setAttribute("getWebUrl", getWebUrl());
		return "pwd-update";
	}
	
	/**
	 * 修改密码
	 * @return
	 */
	public String savePwdUpdate() { 
		try {
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			
			if(null != adminUserDto){
				if (PublicUtil.isNotNullAndEmpty(userId)) {
					
					//UserDto ud = (UserDto) serviceFacade.getUserService().get(UserDto.class, Integer.valueOf(userId));
					AdminUserDto ud = (AdminUserDto)serviceFacade.getAdminUserService().get(AdminUserDto.class, userId);
				
					 if (PublicUtil.isNotNullAndEmpty(userPwd) && userPwd.equals(ud.getUserPwd())) {
						 ud.setUserPwd(newUserPwd);
						 //serviceFacade.getUserService().update(ud); 
						 serviceFacade.getAdminUserService().update(ud);//联盟管理人员
						 println("0");
					 } else {
						 //原始密码错误
						 println("-1");
					 }
				} else {
					//未登录
					 println("-2");
				}
			}else if(null != restaurentUserDto){
				if (PublicUtil.isNotNullAndEmpty(userId)) {
					
					RestaurentUserDto ud = (RestaurentUserDto) serviceFacade.getRestaurantUserService().get(RestaurentUserDto.class, Integer.valueOf(userId));
				
					 if (PublicUtil.isNotNullAndEmpty(userPwd) && userPwd.equals(ud.getPwd())) {
						 ud.setPwd(newUserPwd);
						 serviceFacade.getRestaurantUserService().update(ud);//修改
						 println("0");
					 } else {
						 //原始密码错误
						 println("-1");
					 }
				} else {
					//未登录
					 println("-2");
				}
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		
		return null;
	}
	
	//查询原来的密码,判断输入的原来密码
	public String selectPwd(){
		UserDto userSelectPwd = null;
		try{
			if(PublicUtil.isNotNullAndEmpty(userId)){
				userSelectPwd = (UserDto)serviceFacade.getUserService().get(UserDto.class,Integer.valueOf(userId));
				if(null!=userPwd && !"null".equals(userPwd)){
					if(userPwd.equals(userSelectPwd.getUserPwd())){//输入密码与原来密码相同
						println("0");
					}else{
						println("1");//输入密码与原来密码不同
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 会员过滤信息
	 * @return
	 */
	public String userFilterMsg() {
		return "userFilterMsg";
	}
	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getNewUserPwd() {
		return newUserPwd;
	}

	public void setNewUserPwd(String newUserPwd) {
		this.newUserPwd = newUserPwd;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
}
